`timescale 1ns / 1ps

//////////////////////////////////////////////////////////////////////
////                                                              //// 
////                                                              //// 
////  Parte del proyecto del simple comprobador de memoria        ////  
////                                                              ////
////                                                              //// 
////  Description                                                 //// 
////   - Counting time writing or reading                         //// 
////                                                              //// 
////  To Do:                                                      //// 
////   - Counting time writing or reading                         //// 
////                                                              //// 
////  Author(s):                                                  //// 
////      - Sergio Gonzalez Q, sergiogq@hotmail.es                ////
////      - Alejandro Morales, ale3191@gmail.com                  ////
////                                                              //// 
////////////////////////////////////////////////////////////////////// 
////                                                              //// 
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 //// 
////                                                              //// 
//// This source file may be used and distributed without         //// 
//// restriction provided that this copyright statement is not    //// 
//// removed from the file and that any derivative work contains  //// 
//// the original copyright notice and the associated disclaimer. //// 
////                                                              //// 
//// This source file is free software; you can redistribute it   //// 
//// and/or modify it under the terms of the GNU Lesser General   //// 
//// Public License as published by the Free Software Foundation; //// 
//// either version 2.1 of the License, or (at your option) any   //// 
//// later version.                                               //// 
////                                                              //// 
//// This source is distributed in the hope that it will be       //// 
//// useful, but WITHOUT ANY WARRANTY; without even the implied   //// 
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //// 
//// PURPOSE.  See the GNU Lesser General Public License for more //// 
//// details.                                                     //// 
////                                                              //// 
//// You should have received a copy of the GNU Lesser General    //// 
//// Public License along with this source; if not, download it   //// 
//// from http://www.opencores.org/lgpl.shtml                     //// 
////                                                              ////
//////////////////////////////////////////////////////////////////////

module conterWriteRead(clk_i, rst_i,write_read_i, time_ended_o);
	
	// Input
	input clk_i, rst_i;
	input  write_read_i;
	
	// Output
	output reg time_ended_o;
	
	// 10 MHz --> t= 100 ns
	reg [1:0] counter; // 
	parameter size = 2;

always @(posedge clk_i) //Siempre que hay un cambio en el flanco positivo del reloj
	begin
		if(rst_i) // Si el usuario hace un reset
			begin
				counter<=0; // Empieza a contar en cero
				time_ended_o <= 0;
			end
		else
		begin
			if(write_read_i)
			begin	    
				if(counter == size) // Si ya termino de contar, osea llego a 200 ns
				begin
					counter <= 0; //Resetea el contador 
					time_ended_o <= 1; //Pone la salida en alto
				end 
				else if (counter < size && (!time_ended_o))
					counter <= counter + 1; // Incremento del contador
			end
			else
				time_ended_o <= 0; // Empieza a contar en cero
		end
	end
endmodule
